package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 1260.二维网格迁移
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/7/20 10:20
 */
public class P1260Shift2dGrid {
    public static void main(String[] args) {
        Solution solution = new P1260Shift2dGrid().new Solution();
        int[][] aa = {{1}};
        solution.shiftGrid(aa, 100);
        // TO TEST
    }


    class Solution {
        public List<List<Integer>> shiftGrid(int[][] grid, int k) {

            //m行，n列
            int n = grid[0].length, m = grid.length;

            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    list.add(grid[i][j]);
                }
            }

            int[] ans = new int[list.size()];

            //将list数据从头开始放入移动后的数组中
            for (int i = 0; i < list.size(); i++) {
                if ((i + k) <= list.size() - 1) {
                    ans[i + k] = list.get(i);
                } else {
                    //当要移动的步数超过了list的长度，需要从头开始计算
                    int i1 = (i + k) % list.size();
                    ans[i1] = list.get(i);
                }
            }

            int nums = 1;
            List<Integer> list1 = new ArrayList<>();
            List<List<Integer>> list2 = new ArrayList<>();
            for (int i = 0; i < ans.length; i++) {
                if (nums <= n) {
                    nums++;
                    list1.add(ans[i]);
                    continue;
                } else {
                    list2.add(list1);
                    list1 = new ArrayList<>();
                    list1.add(ans[i]);
                    nums = 2;
                }
            }
            list2.add(list1);

            return list2;
        }
    }
}
